﻿#region Using statements
using System;
using Rectangular.Model.Entities;
using Rectangular.Model.Environment;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
#endregion

namespace Rectangular.Controller
{
    public class PlayerInput
    {
        #region Members
        /* Player */
        private Player _player;
        /* The map */
        private TileMap _map;
        /* Current keyboard state */
        private KeyboardState _keyboardState;
        /* Current mouse state */
        private MouseState _mouseState;
        #endregion

        #region Constructors
        /// <summary>
        /// Constructs a new PlayerInput
        /// </summary>
        public PlayerInput(Player player, TileMap map)
        {
            _player = player;
            _map = map;
        }
        #endregion

        #region Methods
        /// <summary>
        /// Updates player input
        /// </summary>
        public void Update(GameTime gameTime)
        {
            // Get the latest keyboard and mouse state
            _keyboardState = Keyboard.GetState();
            _mouseState = Mouse.GetState();

            #region Mouse buttons
            // Left mouse button
            if (_mouseState.LeftButton == ButtonState.Pressed)
            {
                ProcessLeftMouseButton(gameTime);
            }
            #endregion

            #region Keyboard movement
            // A: Move left
            if (_keyboardState.IsKeyDown(Keys.A))
            {
                ProcessAButton(gameTime);
            }

            // D: Move right
            if (_keyboardState.IsKeyDown(Keys.D))
            {
                ProcessDButton(gameTime);
            }

            // W: Move up
            if (_keyboardState.IsKeyDown(Keys.W))
            {
                ProcessWButton(gameTime);
            }

            // S: Move down
            if (_keyboardState.IsKeyDown(Keys.S))
            {
                ProcessSButton(gameTime);
            }
            #endregion

            #region Keyboard other
            #endregion
        }
        #endregion

        #region Mouse button handlers
        /// <summary>
        /// Logic for when the left mouse button is pressed
        /// </summary>
        private void ProcessLeftMouseButton(GameTime gameTime)
        {
            if (_player.CanFire)
            {
                _player.Fire(new Vector2(_mouseState.X, _mouseState.Y));
            }
        }
        #endregion

        #region Keyboard button handlers
        /// <summary>
        /// Logic for when the A button is pressed
        /// </summary>
        private void ProcessAButton(GameTime gameTime)
        {
            // Move player left
            _player.Position += new Vector2(-_player.MovingSpeed, 0);

            // Check collisions
            CollisionManager.ProcessPlayerVsEnvironmentHorizontal(_player, _map);
        }

        /// <summary>
        /// Logic for when the D button is pressed
        /// </summary>
        private void ProcessDButton(GameTime gameTime)
        {
            // Move player right
            _player.Position += new Vector2(_player.MovingSpeed, 0);

            // Check collisions
            CollisionManager.ProcessPlayerVsEnvironmentHorizontal(_player, _map);
        }

        /// <summary>
        /// Logic for when the W button is pressed
        /// </summary>
        private void ProcessWButton(GameTime gameTime)
        {
            // Move player up
            _player.Position += new Vector2(0, -_player.MovingSpeed);

            // Check collisions
            CollisionManager.ProcessPlayerVsEnvironmentVertical(_player, _map);
        }

        /// <summary>
        /// Logic for when the S button is pressed
        /// </summary>
        private void ProcessSButton(GameTime gameTime)
        {
            // Move player down
            _player.Position += new Vector2(0, _player.MovingSpeed);

            // Check collisions
            CollisionManager.ProcessPlayerVsEnvironmentVertical(_player, _map);
        }
        #endregion
    }
}